package c.e.b.h.e.l;

import com.pgl.sys.ces.out.ISdkLite;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

/* compiled from: QueueFile */
public class c implements Closeable {
    public static final Logger g = Logger.getLogger(c.class.getName());
    public final RandomAccessFile a;
    public int b;
    public int c;
    public b d;
    public b e;
    public final byte[] f = new byte[16];

    /* compiled from: QueueFile */
    public static class b {
        public static final b c = new b(0, 0);
        public final int a;
        public final int b;

        public b(int i, int i2) {
            this.a = i;
            this.b = i2;
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(b.class.getSimpleName());
            stringBuilder.append("[position = ");
            stringBuilder.append(this.a);
            stringBuilder.append(", length = ");
            return c.b.a.a.a.c(stringBuilder, this.b, "]");
        }
    }

    /* compiled from: QueueFile */
    public final class c extends InputStream {
        public int a;
        public int b;

        public c(b bVar, a aVar) {
            int i = bVar.a + 4;
            int i2 = c.this.b;
            if (i >= i2) {
                i = (i + 16) - i2;
            }
            this.a = i;
            this.b = bVar.b;
        }

        public int read(byte[] bArr, int i, int i2) throws IOException {
            Objects.requireNonNull(bArr, "buffer");
            if ((i | i2) < 0 || i2 > bArr.length - i) {
                throw new ArrayIndexOutOfBoundsException();
            }
            int i3 = this.b;
            if (i3 <= 0) {
                return -1;
            }
            if (i2 > i3) {
                i2 = i3;
            }
            c.this.w(this.a, bArr, i, i2);
            this.a = c.a(c.this, this.a + i2);
            this.b -= i2;
            return i2;
        }

        public int read() throws IOException {
            if (this.b == 0) {
                return -1;
            }
            c.this.a.seek((long) this.a);
            int read = c.this.a.read();
            this.a = c.a(c.this, this.a + 1);
            this.b--;
            return read;
        }
    }

    /* compiled from: QueueFile */
    public interface d {
        void a(InputStream inputStream, int i) throws IOException;
    }

    /* compiled from: QueueFile */
    public class a implements d {
        public boolean a = true;
        public final /* synthetic */ StringBuilder b;

        public a(c cVar, StringBuilder stringBuilder) {
            this.b = stringBuilder;
        }

        public void a(InputStream inputStream, int i) throws IOException {
            if (this.a) {
                this.a = false;
            } else {
                this.b.append(", ");
            }
            this.b.append(i);
        }
    }

    public c(File file) throws IOException {
        String str = "rwd";
        if (!file.exists()) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(file.getPath());
            stringBuilder.append(".tmp");
            File file2 = new File(stringBuilder.toString());
            RandomAccessFile randomAccessFile = new RandomAccessFile(file2, str);
            try {
                randomAccessFile.setLength(4096);
                randomAccessFile.seek(0);
                byte[] bArr = new byte[16];
                int[] iArr = new int[]{4096, 0, 0, 0};
                int i = 0;
                for (int i2 = 0; i2 < 4; i2++) {
                    v0(bArr, i, iArr[i2]);
                    i += 4;
                }
                randomAccessFile.write(bArr);
                if (!file2.renameTo(file)) {
                    throw new IOException("Rename failed!");
                }
            } finally {
                randomAccessFile.close();
            }
        }
        RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, str);
        this.a = randomAccessFile2;
        randomAccessFile2.seek(0);
        randomAccessFile2.readFully(this.f);
        int t = t(this.f, 0);
        this.b = t;
        if (((long) t) <= randomAccessFile2.length()) {
            this.c = t(this.f, 4);
            t = t(this.f, 8);
            int t2 = t(this.f, 12);
            this.d = s(t);
            this.e = s(t2);
            return;
        }
        StringBuilder g = c.b.a.a.a.g("File is truncated. Expected length: ");
        g.append(this.b);
        g.append(", Actual length: ");
        g.append(randomAccessFile2.length());
        throw new IOException(g.toString());
    }

    public static int a(c cVar, int i) {
        int i2 = cVar.b;
        return i < i2 ? i : (i + 16) - i2;
    }

    public static int t(byte[] bArr, int i) {
        return ((((bArr[i] & ISdkLite.REGION_UNSET) << 24) + ((bArr[i + 1] & ISdkLite.REGION_UNSET) << 16)) + ((bArr[i + 2] & ISdkLite.REGION_UNSET) << 8)) + (bArr[i + 3] & ISdkLite.REGION_UNSET);
    }

    public static void v0(byte[] bArr, int i, int i2) {
        bArr[i] = (byte) (i2 >> 24);
        bArr[i + 1] = (byte) (i2 >> 16);
        bArr[i + 2] = (byte) (i2 >> 8);
        bArr[i + 3] = (byte) i2;
    }

    public synchronized void close() throws IOException {
        this.a.close();
    }

    public final void h0(int i, byte[] bArr, int i2, int i3) throws IOException {
        int i4 = this.b;
        if (i >= i4) {
            i = (i + 16) - i4;
        }
        if (i + i3 <= i4) {
            this.a.seek((long) i);
            this.a.write(bArr, i2, i3);
            return;
        }
        i4 -= i;
        this.a.seek((long) i);
        this.a.write(bArr, i2, i4);
        this.a.seek(16);
        this.a.write(bArr, i2 + i4, i3 - i4);
    }

    public void k(byte[] bArr) throws IOException {
        int length = bArr.length;
        synchronized (this) {
            if ((0 | length) >= 0) {
                if (length <= bArr.length - 0) {
                    int i;
                    o(length);
                    boolean r = r();
                    if (r) {
                        i = 16;
                    } else {
                        b bVar = this.e;
                        i = t0((bVar.a + 4) + bVar.b);
                    }
                    b bVar2 = new b(i, length);
                    v0(this.f, 0, length);
                    h0(bVar2.a, this.f, 0, 4);
                    h0(bVar2.a + 4, bArr, 0, length);
                    u0(this.b, this.c + 1, r ? bVar2.a : this.d.a, bVar2.a);
                    this.e = bVar2;
                    this.c++;
                    if (r) {
                        this.d = bVar2;
                    }
                }
            }
            throw new IndexOutOfBoundsException();
        }
    }

    public synchronized void n() throws IOException {
        u0(4096, 0, 0, 0);
        this.c = 0;
        b bVar = b.c;
        this.d = bVar;
        this.e = bVar;
        if (this.b > 4096) {
            this.a.setLength((long) 4096);
            this.a.getChannel().force(true);
        }
        this.b = 4096;
    }

    public final void o(int i) throws IOException {
        i += 4;
        int s0 = this.b - s0();
        if (s0 < i) {
            int i2 = this.b;
            do {
                s0 += i2;
                i2 <<= 1;
            } while (s0 < i);
            this.a.setLength((long) i2);
            this.a.getChannel().force(true);
            b bVar = this.e;
            i = t0((bVar.a + 4) + bVar.b);
            if (i < this.d.a) {
                FileChannel channel = this.a.getChannel();
                channel.position((long) this.b);
                long j = (long) (i - 4);
                if (channel.transferTo(16, j, channel) != j) {
                    throw new AssertionError("Copied insufficient number of bytes!");
                }
            }
            i = this.e.a;
            s0 = this.d.a;
            if (i < s0) {
                int i3 = (this.b + i) - 16;
                u0(i2, this.c, s0, i3);
                this.e = new b(i3, this.e.b);
            } else {
                u0(i2, this.c, s0, i);
            }
            this.b = i2;
        }
    }

    public synchronized void q(d dVar) throws IOException {
        int i = this.d.a;
        for (int i2 = 0; i2 < this.c; i2++) {
            b s = s(i);
            dVar.a(new c(s, null), s.b);
            i = t0((s.a + 4) + s.b);
        }
    }

    public synchronized boolean r() {
        return this.c == 0;
    }

    public final b s(int i) throws IOException {
        if (i == 0) {
            return b.c;
        }
        this.a.seek((long) i);
        return new b(i, this.a.readInt());
    }

    public int s0() {
        if (this.c == 0) {
            return 16;
        }
        b bVar = this.e;
        int i = bVar.a;
        int i2 = this.d.a;
        if (i >= i2) {
            return (((i - i2) + 4) + bVar.b) + 16;
        }
        return (((i + 4) + bVar.b) + this.b) - i2;
    }

    public final int t0(int i) {
        int i2 = this.b;
        return i < i2 ? i : (i + 16) - i2;
    }

    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(c.class.getSimpleName());
        stringBuilder.append('[');
        stringBuilder.append("fileLength=");
        stringBuilder.append(this.b);
        stringBuilder.append(", size=");
        stringBuilder.append(this.c);
        stringBuilder.append(", first=");
        stringBuilder.append(this.d);
        stringBuilder.append(", last=");
        stringBuilder.append(this.e);
        stringBuilder.append(", element lengths=[");
        try {
            q(new a(this, stringBuilder));
        } catch (IOException e) {
            g.log(Level.WARNING, "read error", e);
        }
        stringBuilder.append("]]");
        return stringBuilder.toString();
    }

    public final void u0(int i, int i2, int i3, int i4) throws IOException {
        byte[] bArr = this.f;
        r2 = new int[4];
        int i5 = 0;
        r2[0] = i;
        r2[1] = i2;
        r2[2] = i3;
        r2[3] = i4;
        i = 0;
        while (i5 < 4) {
            v0(bArr, i, r2[i5]);
            i += 4;
            i5++;
        }
        this.a.seek(0);
        this.a.write(this.f);
    }

    public synchronized void v() throws IOException {
        if (r()) {
            throw new NoSuchElementException();
        } else if (this.c == 1) {
            n();
        } else {
            b bVar = this.d;
            int t0 = t0((bVar.a + 4) + bVar.b);
            w(t0, this.f, 0, 4);
            int t = t(this.f, 0);
            u0(this.b, this.c - 1, t0, this.e.a);
            this.c--;
            this.d = new b(t0, t);
        }
    }

    public final void w(int i, byte[] bArr, int i2, int i3) throws IOException {
        int i4 = this.b;
        if (i >= i4) {
            i = (i + 16) - i4;
        }
        if (i + i3 <= i4) {
            this.a.seek((long) i);
            this.a.readFully(bArr, i2, i3);
            return;
        }
        i4 -= i;
        this.a.seek((long) i);
        this.a.readFully(bArr, i2, i4);
        this.a.seek(16);
        this.a.readFully(bArr, i2 + i4, i3 - i4);
    }
}
